# Notes about building a rtk-query unit test 1. `createApi()` will ultimately call `buildInitiate()` ## Writeup At first, I want to figure out how the msw fake REST API endpoints are setup, as I do want to simulate proper refetches (or should I make fetch() entirely?). I’m pretty stoned and don’t even know what I want to really test. I changed the code of buildInitiate so that querying an endpoint is kept in an array of runningQueries, not just one. The bug behavior was that a forceRefetch: true call would not be tracked to the end if other requests were already running. The best way to test that is to create a normal query (which sets the cache to pending and fills the runningQueries variable), and then start a refetch (or even better, a manual await initiate({forceRefetch: true}). The actual dispatching of the state into the store is I think triggered in createaAsyncThunk. It first calls `dispatch(pending(...))`. It then calls promise.race for the final action. ## Issues with debugging in Intellij I can’t set breakpoints or inspect module.ts, as it makes IntelliJ freak out. ## How does rte-query work I am tracing the test “passes the extraArgument property to the baseQueryApi” unit test in buildThunks.test.tsx. When initiate() is called, the `createImmutableStateInvariantMiddleware` method is called. According to the documentation, ``` Creates a middleware that checks whether any state was mutated in between dispatches or during a dispatch. If any mutation is detected, an error is thrown ``` As such, it’s probably irrelevant. The next step, when `next` is called buy th middleware, is the `queryAction` from buildInitiate.ts. This is created in `buildInitiateQuery`. queryAction is a function that takes the following arguments: * `args` (the args passed to initiate, i guess) * a default set of options: `subscribe: true`, `forceRefetch: false`, `subcriptionOptions` This returns a closure that takes a `dispatch` and a `getState` method. This closure computes the `queryCacheKey`. It then creates a `queryThunk`, which takes the following arguments: * type : query * subscribe * forceRefetch * subscriptionoptions, * endpointName, * originalArgs: arg, * queryCacheKey The result is then passed to the dispatch closure